﻿Imports System.ComponentModel
Imports System.IO

Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Security
Imports DotNetNuke.Entities.Users
Imports System.Xml


Namespace dnnWerk.Modules.Nuntio.Content
    Public Class ContentBase
        Inherits PortalModuleBase

#Region "Common Helper Properties"

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property SupportedLocales() As LocaleCollection
            Get
                If ShowAllLocales Then
                    SupportedLocales = Localization.GetSupportedLocales()
                Else
                    SupportedLocales = Localization.GetEnabledLocales()
                End If
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property ActiveLocales() As LocaleCollection
            Get
                Return Localization.GetEnabledLocales()
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property ModuleDirectory() As String
            Get
                Return "/Desktopmodules/Nuntio.Content/"
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property CurrentLocale() As String
            Get
                Return CType(Page, PageBase).PageCulture.Name
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property CanEdit() As Boolean
            Get
                Return PortalSecurity.HasNecessaryPermission(SecurityAccessLevel.Edit, PortalSettings, Me.ModuleConfiguration, UserInfo.Username)
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property CanSubmit() As Boolean
            Get
                Return PortalSecurity.HasNecessaryPermission(SecurityAccessLevel.Edit, PortalSettings, Me.ModuleConfiguration, UserInfo.Username)
            End Get
        End Property

#End Region

#Region "Settings"

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property StripLocaleBrackets() As Boolean
            Get
                Return True
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property IncludeLocaleIcon() As Boolean
            Get
                Return True
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property ShowAllLocales() As Boolean
            Get
                Dim Setting As Object = Personalization.Personalization.GetProfile("NC_SETTINGS", "SHOW_ALL_LOCALES")
                If Not Setting Is Nothing Then
                    Select Case Setting.ToString.ToUpper
                        Case "TRUE"
                            Return True
                        Case "FALSE"
                            Return False
                        Case Else
                            Return False
                    End Select
                End If
                Return False
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property DefaultFallback() As String
            Get
                Dim Setting As Object = Personalization.Personalization.GetProfile("NC_SETTINGS", "DEFAULT_FALLBACK")
                If Not Setting Is Nothing Then
                    Return Setting.ToString
                End If
                Return "NONE"
            End Get
        End Property

#End Region

#Region "Helper Methods"

        Public Sub SynchronizePortalStore(ByVal PortalId As Integer, ByVal AdministratorRoleId As Integer, ByVal HomeDirectory As String)

            Dim tmp As String = HomeDirectory & "\diurhhgftgtahfgtrgfd"
            System.IO.Directory.CreateDirectory(tmp)
            If System.IO.Directory.Exists(tmp) Then
                System.IO.Directory.Delete(tmp)
            End If

            FileSystemUtils.Synchronize(PortalId, AdministratorRoleId, HomeDirectory)

        End Sub

        Protected Sub BindFallbackLocales(ByRef drp As DropDownList)

            drp.Items.Clear()
            drp.Items.Add(New ListItem("No Fallback (hide module)", "-1"))

            Dim i As Integer
            Dim info As System.Globalization.CultureInfo
            For i = 0 To ActiveLocales.Count - 1

                info = System.Globalization.CultureInfo.CreateSpecificCulture(CType(SupportedLocales(i).Value, Locale).Code)
                drp.Items.Add(New ListItem(info.DisplayName, info.Name))

            Next

        End Sub

        Protected Function GetLocaleDisplay(ByVal Locale As System.Globalization.CultureInfo) As String

            Dim strLocaleString As String = ""
            Dim strLocaleShort As String = Locale.Name.Substring(Locale.Name.IndexOf("-") + 1)
            Dim strLocaleName As String = Locale.DisplayName

            If StripLocaleBrackets Then
                Try
                    strLocaleName = Locale.DisplayName.Substring(0, Locale.DisplayName.IndexOf("(") - 1).Trim
                Catch
                End Try
            End If

            If IncludeLocaleIcon Then
                strLocaleString += "<img src=""" & Me.ModuleDirectory & "images/flags/" & strLocaleShort & ".gif"" alt=""" & strLocaleShort & """ />"
            End If

            strLocaleString += strLocaleName

            Return strLocaleString

        End Function

#End Region

    End Class
End Namespace

